﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace Emissor
{
    public partial class frmTestesBluetooth : Form
    {
        SerialPort sp1;
        byte[] buffer = new byte[5];
        Int32 valor;
        Timer t1;
        int pos = 0;
        
        public frmTestesBluetooth()
        {
            InitializeComponent();

            //sp1 = new SerialPort();
            //sp1 = Initialize(sp1);

            //sp1.Open();
            //sp1.DiscardInBuffer();
            //sp1.DiscardOutBuffer();
            //sp1.DiscardNull = false;
            //sp1.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);

            t1 = new Timer();
            t1.Tick += new EventHandler(t1_Tick);
            t1.Interval = 100;
            t1.Start();
        }

        void t1_Tick(object sender, EventArgs e)
        {
            var ph = pctBox.Size.Height;
            var w = pctBox.Size.Width / 2;
            var h = pctBox.Size.Height / 2;
            Graphics objGraphics = this.pctBox.CreateGraphics();
            Pen pen = new Pen(Color.Black);
            h = h + valor;
            objGraphics.DrawLine(pen, new Point(pos, h), new Point(++pos, h));
        }

        private SerialPort Initialize(SerialPort sp)
        {
            sp.BaudRate = 115200;
            sp.ReadBufferSize = 200;
            sp.WriteBufferSize = 200;
            sp.DataBits = 8;
            sp.Parity = Parity.None;
            sp.StopBits = StopBits.One;
            sp.ReadTimeout = 1000;
            sp.ReceivedBytesThreshold = 5;

            return sp;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {

        }

        void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            sp1.Read(buffer, 0, 5);

            valor = System.BitConverter.ToInt32(buffer, 0);
        }

        public void Desenhar(Object stateInfo)
        {

        }
    }
}
